#!/usr/bin/env bash
age() {
#echo "Usage: ${0##*/} (start|stop|restart|check|log|vim) (api|admin|task|schedule|mlfsync|monitor) (dev|test|prev|prod)"
exit 1
}

ACTION=$1
SERVER=$2
PROFILE="test"

[ $# -gt 1 ] || usage
source /etc/profile
APP_PATH="/storage/local-media/servers"
OPERATION_LOG_FILE="/storage/local-media/logs/operation.log"
MONITOR_LOG_FILE="/storage/local-media/logs/monitoring.log"
LOG_FILE="/storage/local-media/logs/$SERVER"

PROGRAM_ARGS="--spring.profiles.active=$PROFILE --spring.application.name=localmedia-$SERVER --log.path=$LOG_FILE --operation_log.file=$OPERATION_LOG_FILE --monitor_log.file=$MONITOR_LOG_FILE"

PID=

case ${SERVER} in
admin)
PORT=8080
DEBUG_PORT=9090
JAVA_OPTIONS="-Xmx512m"
;;
api)
PORT=8081
DEBUG_PORT=9091
JAVA_OPTIONS="-Xmx512m"
;;
trans)
PORT=8082
DEBUG_PORT=9092
JAVA_OPTIONS="-Xmx2048m"
;;
product)
PORT=8083
DEBUG_PORT=9093
JAVA_OPTIONS="-Xmx512m"
;;
search)
PORT=8084
DEBUG_PORT=9094
JAVA_OPTIONS="-Xmx512m"
;;
xhs)
PORT=8085
DEBUG_PORT=9095
JAVA_OPTIONS="-Xmx512m"
;;
spider)
PORT=8086
DEBUG_PORT=9096
JAVA_OPTIONS="-Xmx2048m"
;;
szbbm)
PORT=8087
DEBUG_PORT=9097
JAVA_OPTIONS="-Xmx256m"
;;

    *)
        usage
    ;;
esac

PROGRAM_ARGS="--server.port=$PORT $PROGRAM_ARGS $EXTRA_ARGS"

get_pid(){
PID=`ps -ef | grep -v grep | grep localmedia-${SERVER} | awk '{print $2}'`
}

cat_start(){
get_pid
if [[ -n ${PID} ]] ;
then
echo -e "\033[41;37m  ERROR: App $SERVER is still RUNNING. PID:$PID \033[0m"
exit 1
fi

    if [ ${DEBUG_PORT} ];
then
export JAVA_OPTS="-server  -Dim4java.useGM=true $JAVA_OPTIONS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$DEBUG_PORT"
else
export JAVA_OPTS="-server  -Dim4java.useGM=true $JAVA_OPTIONS"
fi

    echo -e "\033[32m  App $SERVER is starting: java $JAVA_OPTS -jar localmedia-$SERVER.jar $PROGRAM_ARGS \033[0m"
    echo "/usr/local/jdk/bin/java ${JAVA_OPTS} -jar ${APP_PATH}/localmedia-${SERVER}.jar ${PROGRAM_ARGS} > /dev/null 2>&1 &"
    nohup /usr/local/jdk/bin/java ${JAVA_OPTS} -jar ${APP_PATH}/localmedia-${SERVER}.jar ${PROGRAM_ARGS} > /dev/null 2>&1 &
    get_pid
    echo -e "\033[32m  App $SERVER started now. PID:$PID \033[0m"
    #tail -f "$LOG_PATH"
}
cat_stop(){
get_pid
if [[ -n ${PID} ]] ;
then
echo -e "\033[31m  App $SERVER[PID:$PID] is stopping... \033[31m"
curl -connect-timeout 2 -m 2  -s -X POST --referer http://localhost:${PORT} http://localhost:${PORT}/user/login
echo -e ""

        get_pid
        if [[ -n ${PID} ]] ;
        then
            kill $PID
            sleep 2.5
        fi
    
        get_pid
        if [[ -n ${PID} ]] ;
        then
            kill $PID
            sleep 2
        fi

        get_pid
        if [[ -n ${PID} ]] ;
        then
            echo -e "\033[41;37m  ERROR: Force to shutdown $SERVER. PID:$PID \033[0m"
            kill -9 $PID
        fi
        
        echo -e "\033[31m  App $SERVER[PID:$PID] stopped. \033[0m"
    else
        echo -e "\033[31m  WARN: App $SERVER is not running. \033[31m"
    fi
}
cat_restart(){
#if [[ -n ${PROFILE} ]] ;
#then
#    echo "\033[41;37m  ERROR: Profile is not specified! \033[0m"
#    exit 1
#fi
echo -e "\033[33m  App $SERVER is restarting... \033[0m"
cat_stop
cat_start
echo -e "\033[33m  App $SERVER restarted. \033[0m"
echo -e ""
}
cat_log(){
tail -f ${LOG_FILE}/all.log -n 500
}
case "$ACTION" in
start)
cat_start
;;
stop)
cat_stop
;;
restart)
cat_restart
;;
check)
ps -ef | grep "localmedia-$SERVER" | grep -v "grep"
;;
log)
cat_log
;;
vim)
vim ${LOG_PATH}
;;
*)
usage
;;
esac